<template>
  <a-modal :width="modalWidth"
           :title="title"
           :visible.sync="visible"
           @cancel="handleClose">
    <template>
        <a-card :body-style="{padding: '24px 32px'}" :bordered="false" class="ukeyModal">
            <a-form :form="form">
                <a-row :gutter="24">
                  <a-col :md="12" :sm="12">
                      <a-form-item label="超级密码" :labelCol="{span: 7}" :wrapperCol="{span: 10}">
                          <a-input disabled v-model="strSuperPin"/>
                      </a-form-item>
                  </a-col>
                  <a-col :md="12" :sm="12">
                      <a-form-item label="新超级密码" :labelCol="{span: 7}" :wrapperCol="{span: 10}">
                          <a-input disabled v-model="strNewSuperPin"/>
                      </a-form-item>
                  </a-col>
                </a-row>
                <a-row :gutter="24">
                  <a-col :md="12" :sm="12">
                      <a-form-item label="用户密码" :labelCol="{span: 7}" :wrapperCol="{span: 10}">
                          <a-input v-model="strUserPin"/>
                      </a-form-item>
                  </a-col>
                  <a-col :md="12" :sm="12">
                      <a-form-item label="种子码" :labelCol="{span: 7}" :wrapperCol="{span: 10}">
                          <a-input disabled v-model="strSeed"/>
                      </a-form-item>
                  </a-col>
                </a-row>
                <a-row :gutter="24">
                  <a-col :md="12" :sm="12">
                      <a-form-item label="3DES密钥" :labelCol="{span: 7}" :wrapperCol="{span: 10}">
                          <a-input disabled v-model="strPriKey"/>
                      </a-form-item>
                  </a-col>
                  <a-col :md="12" :sm="12">
                      <a-form-item label="PIN码" :labelCol="{span: 7}" :wrapperCol="{span: 10}">
                        <a-input disabled v-model="uid"/>
                      </a-form-item>
                  </a-col>
                </a-row>
                <a-row :gutter="24">
                  <a-col :md="12" :sm="12">
                      <a-form-item label="公司名称" :labelCol="{span: 7}" :wrapperCol="{span: 10}">
                          <a-input v-model="company"/>
                      </a-form-item>
                  </a-col>
                  <a-col :md="12" :sm="12">
                      <a-form-item label="应用网址" :labelCol="{span: 7}" :wrapperCol="{span: 10}">
                          <a-input v-model="url"/>
                      </a-form-item>
                  </a-col>
                </a-row>
                <a-row :gutter="24">
                  <a-col :md="12" :sm="12" class="ukey-form">
                      <a-form-item label="用 户 名" :labelCol="{span: 7}" :wrapperCol="{span: 10}">
                          <a-input v-model="account"/>
                      </a-form-item>
                  </a-col>
                  <a-col :md="12" :sm="12" class="ukey-form">
                      <a-form-item label="ukey别名（单位名称）" :labelCol="{span: 7}" :wrapperCol="{span: 10}">
                          <a-input v-model="orgName"/> <font class="red">写入key,登录时显示为用户名</font>
                      </a-form-item>
                  </a-col>
                </a-row>
                <a-row :gutter="24">
                  <a-col :md="12" :sm="12" class="ukey-form">
                      <a-form-item label="ukey备注" :labelCol="{span: 7}" :wrapperCol="{span: 10}">
                        <a-input v-model="other"/> <font class="red">写入key,key备注信息</font>
                      </a-form-item>
                  </a-col>
                  <a-col :md="12" :sm="12" class="ukey-form">
                      <a-form-item label="使用说明" :labelCol="{span: 7}" :wrapperCol="{span: 10}">
                        <a-input v-model="instruction"/> <font class="red">写入到数据库，用于标明ukey用途</font>
                      </a-form-item>
                  </a-col>
                </a-row>
            </a-form>
            <!--<div class="drawer-bootom-button" style="width:100%; margin-top: 50px; padding-bottom: 20px" align="center">
                <a-button @click="createUser" type="primary" >创建用户</a-button>
                <a-button style="margin-left: 50px" @click="delUser" type="primary" >删除用户</a-button>
            </div>-->
        </a-card>
    </template>
    <template slot="footer">
        <a-button @click="createUser" type="primary" >创建用户</a-button>
        <!--<a-button style="margin-left: 50px" @click="delUser" type="primary" >删除用户</a-button>-->
    </template>
  </a-modal>
</template>

<script>
    import {IA300_AdminCheckExist,createAdminElementIA300,DetectIA300AdminPlugin,IA300_AdminGetUIDEx,IA300_SetParameters,IA300_EditWithParametersEx,IA300_AdminGetLastError,IA300_AdminGetUID} from '@/utils/IA300AdminJavascript.js';
    import {IA300_GetName,IA300_GetUrl,IA300_GetDescription,IA300_GetOther} from '@/utils/IA300ClientJavascript.js';
    import {getUserInfo,iakeyCreate,iakeyDel} from '@/api/system'
    import {message} from 'ant-design-vue';

    export default {
        name: "iakeyForm",
        data() {
            return {
                //创建表单
                form:this.$form.createForm(this),
                modalWidth:1200,
                visible: false,
                title:"Ukey初始化",
                //实体对象
                iakey: {},
                strSuperPin:"",
                strNewSuperPin:"",
                strUserPin:"",
                strSeed:"",
                strPriKey:"",
                uid:"",
                orgName:"",
                company:"",
                url:"",
                account:"",
                other:"",
                instruction:""
            };
        },
        created(){
            this.$nextTick(() => {
                //表单设置默认值
                //this.form.setFieldsValue({"strSuperPin":"admin"});
                this.strSuperPin = "admin";
                this.strNewSuperPin = "admin";
                this.strUserPin = "12345678";
                this.strSeed = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF";
                this.strPriKey = "ABCDEFGhijklmn0123456789";
                this.company = "河南金明源信息技术有限公司";
            });
            //createAdminElementIA300() 对本页面加入IA300插件
            createAdminElementIA300();
            //DetectActiveX() 判断IA300Admin是否安装
            var create = DetectIA300AdminPlugin();
            if(create == false)
            {
                message.error("IA300插件未安装");
                return false;
            }


            var rtn = IA300_AdminCheckExist();
            if(0<rtn) {
                //var id = IA300_AdminGetUID();
                var id = IA300_AdminGetUIDEx(0);
                //提交到后台
                let param = {};
                param.uid = id;
                getUserInfo(param).then((res) => {
                    this.$nextTick(() => {
                        if(res.result == null){
                            this.uid= id;
                            var name = IA300_GetName();
                            if(name == null || name == '' || name == " "){
                                name = IA300_GetOther();
                                if(name == null || name == '' || name == " "){
                                    name = '已从设备读取';
                                }
                            }
                            this.orgName = name;
                            //公司信息
                            this.company = IA300_GetDescription();
                            //url地址
                            this.url = IA300_GetUrl();
                            //IA300_GetHardwareId();
                        }else{
                            this.uid= id;
                            this.strSuperPin = res.result.userPin;
                            this.strNewSuperPin = res.result.userPin;
                            this.url = res.result.appUrl;
                            this.account = res.result.account;
                            this.orgName = res.result.keyName;
                            this.instruction = res.result.instruction;
                            /*this.form.setFieldsValue({'strUserPin':res.result.strUserPin});
                            this.form.setFieldsValue({'url':res.result.url});
                            this.form.setFieldsValue({'account':res.result.account});
                            this.form.setFieldsValue({'orgName':res.result.orgName});
                            this.form.setFieldsValue({'instruction':res.result.instruction});*/

                        }

                    });
                })
            }
        },
        methods: {
            view() {
              let that = this;
              that.visible = true;
            },
            createUser(){
                if(this.uid == null || this.uid == ""){
                  message.error("未读取到pin码！");
                  return false;
                }
                if(this.account == null || this.account == ""){
                    message.error("请输入用户名！");
                    return false;
                }
                let param = {};
                param.seed = this.strSeed;
                param.deskey = this.strPriKey;
                param.account = this.account;
                param.uid = this.uid;
                param.userPin = this.strUserPin;
                param.appUrl = this.url;
                param.orgName = this.orgName;
                param.company = this.company;
                param.instruction = this.instruction;
                //提交到后台
                iakeyCreate(param).then((res) => {
                    if(res.sign=="err"){
                        message.error(res.message)
                    }else {
                        var hID = this.uid;
                        var strSuperPin = this.strSuperPin;
                        var strNewSuperPin = this.strNewSuperPin;
                        var strUserPin = this.strUserPin;
                        var strSeed = this.strSeed;
                        var strPriKey = this.strPriKey;
                        var strDescripion = this.company;
                        var strUrl = this.url;
                        var strOther = this.other;
                        var nUserIE = 1;
                        var nEnableRemote = 0;
                        var strKeyName = res.orgName;

                        var rtn = IA300_SetParameters(strNewSuperPin, strUserPin, strSeed, strPriKey, strKeyName, strDescripion, strUrl, strOther, nUserIE, nEnableRemote);
                        if(0 != rtn){
                            message.error("配置错误， 请重新配置参数");
                            return false;
                        }

                        rtn = IA300_EditWithParametersEx(hID, strSuperPin);
                        if(0 != rtn){
                            //message.err("本地写Key失败,错误码:"+IA300_AdminGetLastError());
                            message.error("本地写Key失败,错误码:"+IA300_AdminGetLastError());
                            return false;
                        }
                        message.success(res.orgName+"的UKEY绑定成功");
                        this.visible = false;
                        this.form.resetFields();
                        this.$emit('ok');
                    }
                })
            },
            delUser(){
                if(this.uid == null || this.uid == ""){
                  message.error("未读取到pin码！");
                  return false;
                }else{
                  let param = {};
                  param.serverIaguid = this.uid;
                  iakeyDel(param).then((res) => {
                      if (res.flag) {
                          this.$message.success(res.message);
                          this.$emit('ok');
                      } else {
                          this.$message.warning(res.message);
                      }
                  })
                }
            },
            handleClose() {
              this.visible = false;
              this.form.resetFields();
            }
        },
    };
</script>
<style>
  .ukeyModal .ant-form-item-children{
    display: inline-block;
    line-height: 26px;
  }
  .ukey-form .ant-form-item{
    margin-bottom: 0 !important;
  }
</style>